package kd.bos.openapi.demo.webservice;

import kd.bos.openapi.sdk.util.HttpClientUtil;
import kd.bos.openapi.sdk.util.PropertyUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author johnz
 * @Deacription 通过http调用OpenAPI接口
 * @date 2023年10月20日
 */
public class FindOrderCallByHttpDemo {
    private final static Logger log = Logger.getLogger(FindOrderCallByHttpDemo.class.getName());
    // 苍穹OpenAPI调用接口的URL前缀
    public static final String urlPrefix = PropertyUtil.getProperty("bos.env.cosmic");
    // 基本认证参数前缀
    public static final String OPEN_API_SIGN = "/$/openApiSign/";
    // 基本认证secret key
    public static final String OPEN_API_SIGN_SECRET_KEY = PropertyUtil.getProperty("digestAuth.openApiSign");
    //开放平台接口地址
    public static String openApiUrl = "kapi/v2/kdtest/open/openapi_unittest/findOrders";

    public static void main(String[] args) {
        findOrderByHttp();
    }

    /**
     * 查询订单
     *
     * @return
     * @throws Exception
     */
    public static void findOrderByHttp() {
        log.info("------------------------findOrderByHttp start");
        // 接口地址
        String url = urlPrefix + openApiUrl + OPEN_API_SIGN + OPEN_API_SIGN_SECRET_KEY;
        // 请求报文可以直接从API接口，点API测试后，切换request格式为soap1.2, 直接复制到下面替换
        String soap1_2_request = "<soapenv:Envelope  xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:kapi=\"http://www.kingdee.coms\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <kapi:int>\n" +
                "\t\t\t<data>\n" +
                "\t\t\t\t<billno>003</billno>\n" +
                "\t\t\t</data>\n" +
                "\t\t\t<pageSize>10</pageSize>\n" +
                "\t\t\t<pageNo>1</pageNo>\n" +
                "      </kapi:int>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        Map<String, String> header = new HashMap<>();
        String body = soap1_2_request;
        header.put("Content-Type", "application/soap+xml");
        log.info("------------------------request.body------------------------");
        log.info(body);
        String result = HttpClientUtil.post(url, header, body, false);
        log.info("------------------------response------------------------");
        log.info(result);
        log.info("------------------------findOrderByHttp end");
    }
}